package kd.lxj.util;

import java.net.ConnectException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

public class HTTPUtil {
	
	private static final String CONTENT_TYPE = "Content-Type";
    private static final String APPLICATION = "application/json";
	
	//地磅接口调用方法
	public static String doPostByHttpClient(String url, String data) throws ConnectException {
		try {
			// System.setProperty("javax.net.debug", "ssl");
			DefaultHttpClient httpClient = null;
			if (url.toLowerCase().startsWith("https://")) {
//				System.out.println("use ssl");
				httpClient = new SSLClient();
			} else {
//				System.out.println("use nonssl");
				httpClient = new DefaultHttpClient();
			}
			HttpPost httpPost = new HttpPost(url);
			httpPost.addHeader("Content-Type", "application/json; charset=UTF-8");
			httpPost.addHeader("User-Agent", "qianlima");
			httpPost.addHeader("sign", "9044X7nu34IjHLD9531LeQTVIy2tBFiYx9hlv930Q5kWfgHlCg");
//			httpPost.addHeader("User-Agent", "qianlima");
			StringEntity se = new StringEntity(data, "UTF-8");
			se.setContentType("text/json");
			se.setContentEncoding(new BasicHeader("Content-Type", "application/json; charset=UTF-8"));
			httpPost.setEntity(se);
			HttpResponse response = httpClient.execute(httpPost);
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode != 400 && statusCode != 200) {
				throw new ConnectException("连接服务器发生错误！");
			}
			System.out.println("==statusCode=="+statusCode);
			String body = EntityUtils.toString(response.getEntity());
			System.out.println(body);
			return body;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(" ===== doPostByHttpClient() ERROR ===== ");
			throw new ConnectException(e.getMessage());
		} finally {
			System.clearProperty("javax.net.debug");
		}
	}
	
		//统一门户接口调用方法
		public static String doPostByHttpClientToSSO(String url, String data) throws ConnectException {
			try {
				DefaultHttpClient httpClient = null;
				if (url.toLowerCase().startsWith("https://")) {
					httpClient = new SSLClient();
				} else {
					httpClient = new DefaultHttpClient();
				}
				HttpPost httpPost = new HttpPost(url);
				httpPost.addHeader("Content-Type", "application/json; charset=UTF-8");
				httpPost.addHeader("Accept", "application/json;charset=UTF-8");
				StringEntity se = new StringEntity(data, "UTF-8");
				se.setContentType("text/json");
				se.setContentEncoding(new BasicHeader("Content-Type", "application/json; charset=UTF-8"));
				httpPost.setEntity(se);
				HttpResponse response = httpClient.execute(httpPost);
				int statusCode = response.getStatusLine().getStatusCode();
				if (statusCode != 400 && statusCode != 200) {
					throw new ConnectException("连接服务器发生错误！");
				}
				String body = EntityUtils.toString(response.getEntity());
				return body;
			} catch (Exception e) {
				e.printStackTrace();
				throw new ConnectException(e.getMessage());
			} finally {
				System.clearProperty("javax.net.debug");
			}
		}
		//GET请求
		public static String doGetByHttpClient(String url) throws ConnectException {
			try {
				DefaultHttpClient httpClient = null;
				if (url.toLowerCase().startsWith("https://")) {
					httpClient = new SSLClient();
				} else {
					httpClient = new DefaultHttpClient();
				}
				HttpGet httpGet = new HttpGet(url);
				httpGet.addHeader("Content-Type", "application/json; charset=UTF-8");
				httpGet.addHeader("Accept", "application/json;charset=UTF-8");
				HttpResponse response = httpClient.execute(httpGet);
				int statusCode = response.getStatusLine().getStatusCode();
				if (statusCode != 200) {
					throw new ConnectException("连接服务器发生错误！");
				}
				String body = EntityUtils.toString(response.getEntity());
				System.out.println(body);
				return body;
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println(" ===== doPostByHttpClient() ERROR ===== ");
				throw new ConnectException(e.getMessage());
			} finally {
				System.clearProperty("javax.net.debug");
			}
		}
		

}
